home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Games Machine 103
/
XENIATGM103.iso
/
Shareware
/
GoLive 5.0
/
MM9.Cab
/
F1495_ado.actions5.asp.9B3B646D_CB56_4EAE_BAB7_3E7E8E41A649
< prev
next >
Wrap
Text File
|
2000-08-17
|
22KB
|
687 lines
<!-- #INCLUDE FILE="../include/utils.runtime5.asp" -->
<!-- #INCLUDE FILE="../include/ado.runtime5.asp" -->
<SCRIPT runat="server" language="VBScript">
' *****************************************************************************
'
' include/ado.actions5.asp
'
' Dynamic Link runtime support for Microsoft ADO.
'
' COPYRIGHT (c) 1999-2000 Adobe Systems Incorporated. All rights reserved.
'
' -----------------------------------------------------------------------------
dim setup
setup = false
' -----------------------------------------------------------------------------
' SetupADOAction
' -----------------------------------------------------------------------------
function SetupADOAction(contentSource, allRecords)
if False = RuntimeDebug then
on error resume next
end if
if setup then
exit function
end if
setup = true
'Close parent <input>
Response.Write(">")
call CSWRecordSet_SetupForm( contentSource )
if allRecords then
contentSource.MoveFirst
if 0 = contentSource.BlockSize then
while not contentSource.EOF
keyName = chr(34) & "$key[" & contentSource.AbsolutePosition & "]" & chr(34)
key = chr(34) & contentSource.Key() & chr(34)
Response.Write("<input type=" & chr(34) & "hidden" & chr(34) & " name=" & keyName & " value=" & key & ">")
contentSource.MoveNext
wend
else
index = GetRecordIndex()
contentSource.Move index - 1
while ( not contentSource.EOF ) and ( contentSource.AbsolutePosition <= ( index + contentSource.BlockSize -1 ))
keyName = "'$key[" & contentSource.AbsolutePosition & "]'"
key = chr(34) & contentSource.Key() & chr(34)
Response.Write("<input type=" & chr(34) & "hidden" & chr(34) & " name=" & keyName & " value=" & key & ">")
contentSource.MoveNext
wend
end if
else
keyName = chr(34) & "$key" & chr(34)
key = chr(34) & contentSource.Key() & chr(34)
Response.Write("<input type=" & chr(34) & "hidden" & chr(34) & " name=" & keyName & " value=" & key & ">")
end if
' Protect original close of parent <input>
Response.Write("<")
Response.Write("wbr")
end function
' -----------------------------------------------------------------------------
' SubmitChanges
' -----------------------------------------------------------------------------
function SubmitChanges()
ActionSubmitChanges
end function
' -----------------------------------------------------------------------------
' AddRecord
' -----------------------------------------------------------------------------
function AddRecord()
ActionAddRecord
end function
' -----------------------------------------------------------------------------
' DeleteRecord
' -----------------------------------------------------------------------------
function DeleteRecord()
ActionDeleteRecord
end function
' -----------------------------------------------------------------------------
' DuplicateRecord
' -----------------------------------------------------------------------------
function DuplicateRecord()
ActionDuplicateRecord
end function
' -----------------------------------------------------------------------------
' Dispatch
' -----------------------------------------------------------------------------
if InStr(Request.ServerVariables("PATH_TRANSLATED"), "ado.actions5.asp") then
if Request("$SubmitChanges").Count > 0 or Request("$SubmitChanges.x").Count > 0 then
SubmitChanges
elseif Request("$AddRecord").Count > 0 or Request("$AddRecord.x").Count > 0 then
AddRecord
elseif Request("$DeleteRecord").Count > 0 or Request("$DeleteRecord.x").Count > 0 then
DeleteRecord
elseif Request("$DuplicateRecord").Count > 0 or Request("$DuplicateRecord.x").Count > 0 then
DuplicateRecord
else ' default action
SubmitChanges
end if
end if
</SCRIPT>
<SCRIPT runat="server" language="JScript">
// *****************************************************************************
// SERVER SIDE ACTIONS
//
// -----------------------------------------------------------------------------
// Action handler functions
function ActionSubmitChanges()
{
var source = unobscure( Request.Form( "$source" ));
var connect = unobscure( Request.Form( "$connect" ));
var key = unescape( Request.Form( "$key" ));
var tableName = getTableNameFromSource( source );
var connection = new ActiveXObject( "ADODB.Connection" );
connection.open( connect );
if( key == "$newRecord" ){
addRecordUsingPostDataInsertQuery( connection, tableName );
}else{
updateUsingPostDataUpdateQuery( connection, tableName );
}
Response.Redirect(Request.Form( "$return" ));
return;
}
function ActionDeleteRecord()
{
var source = unobscure( Request.Form( "$source" ));
var connect = unobscure( Request.Form( "$connect" ));
var tableName = getTableNameFromSource( source );
var connection = new ActiveXObject( "ADODB.Connection" );
var returnURL = Request.Form( "$return" );
var currentRecordIndex = getCurrentRecordIndexFromURL( returnURL );
connection.open( connect );
var recordCount = getRecordCountBySelectQuery( connection, tableName );
var redirectURL = setRecordIndexInURL( returnURL, Math.min(currentRecordIndex, recordCount-1 ));
deleteUsingPostDataDeleteQuery( connection, tableName );
Response.Redirect( redirectURL );
return;
}
function ActionAddRecord()
{
var source = unobscure( Request.Form( "$source" ));
var connect = unobscure( Request.Form( "$connect" ));
var tableName = getTableNameFromSource( source );
var connection = new ActiveXObject( "ADODB.Connection" );
var returnURL = Request.Form( "$return" );
var currentRecordIndex = getCurrentRecordIndexFromURL( returnURL );
connection.open( connect );
var recordCount = getRecordCountBySelectQuery( connection, tableName );
var redirectURL = setRecordIndexInURL( returnURL, recordCount+1 );
addRecordUsingPostDataInsertQuery( connection, tableName );
Response.Redirect( redirectURL );
return;
}
function ActionDuplicateRecord()
{
ActionAddRecord();
}
function getRecordCountBySelectQuery( connection, tableName )
{
var sqlCommand = "SELECT COUNT(0) FROM " + tableName;
var result=connection.Execute( sqlCommand );
var ret = result.Fields.Item(0).Value;
return ret;
}
function getCurrentRecordIndexFromURL( URL )
{
var recordIndex = 1;
var currentURL = new String( URL );
var a = currentURL.indexOf( "RECORD_INDEX" );
if( a == -1 ){
a = currentURL.indexOf("RECORD%5FINDEX"); // URL-encoded '_'
}
if( 1 < a ){ // RECORD_INDEX was found
var b = currentURL.indexOf( "=", a );
if( a < b ){
var c = currentURL.indexOf( "&", b );
if( b < c ){
recordIndex = Number( currentURL.substring( b+1, c ));
} else { // RECORD_INDEX was last parameter
recordIndex = Number( currentURL.substring( b+1, currentURL.length ));
}
}
}
return recordIndex;
}
function setRecordIndexInURL( originalURL, nextRecordIndex )
{
var queryString = new String( originalURL );
var returnURL = new String( queryString );
var a = queryString.indexOf( "RECORD_INDEX" );
if( a == -1 ){
a = queryString.indexOf( "RECORD%5FINDEX" ); // URL-encoded '_'
}
if( 1 < a ){ // RECORD_INDEX was found
var b = queryString.indexOf( "=", a );
if( a < b ){
var c = queryString.indexOf( "&", b );
if( b < c ){
returnURL = queryString.substring( 0, a ) + "RECORD_INDEX=" + nextRecordIndex + queryString.substring( c, queryString.length );
}else{ // RECORD_INDEX was last parameter
returnURL = queryString.substring( 0, a ) + "RECORD_INDEX=" + nextRecordIndex;
}
}
}else{ // RECORD_INDEX was not found
if(( queryString.lastIndexOf( "?" ) == queryString.length-1 ) || // ending with "?"
( queryString.lastIndexOf("&") == queryString.length-1 )) { // ending with "&"
returnURL = queryString + "RECORD_INDEX=" + nextRecordIndex;
} else {
if( 1 < queryString.indexOf( "?" )){
returnURL = queryString + "&RECORD_INDEX=" + nextRecordIndex;
}else{
returnURL = queryString + "?RECORD_INDEX=" + nextRecordIndex; // no other URL parameter
}
}
}
return returnURL;
}
function encodeSQL( string )
{
return string.split("'").join("''");
}
// *****************************************************************************
// wrapSQLFieldValue
// mode: 0 / just put blanket
// 1 / Where clause
function wrapSQLFieldValue( connection, value, datatype, mode )
{
var retString = "";
var tempValue = encodeSQL( value );
var wildCardChar = "";
var prefix1999 = "";
var suffix1999 = "";
var prefix2000 = "";
var suffix2000 = "";
switch( String( connection.Properties.Item( "Provider Friendly Name" ))){
case "Microsoft OLE DB Provider for ODBC Drivers":
switch( String( connection.Properties.Item( "DBMS Name" ))){
case "ACCESS":
wildCardChar = "%";
prefix1999 = "#";
suffix1999 = "#";
prefix2000 = "#";
suffix2000 = "#";
break;
case "Oracle8":
case "Oracle":
wildCardChar = "%";
prefix1999 = "to_date('";
suffix1999 = "','yyyy/mm/dd hh24:mi:ss')";
prefix2000 = "to_date('";
suffix2000 = "','yyyy/mm/dd hh24:mi:ss')";
break;
case "Microsoft SQL Server":
wildCardChar = "%";
prefix1999 = "'";
suffix1999 = "'";
prefix2000 = "'";
suffix2000 = "'";
break;
default:
if( RuntimeDebug ){
RuntimeDebugMessage( "Unsupported ODBC driver: \"DBMS Name\" = [" + connection.Properties.Item( "DBMS Name" ) + "]<br>\n" );
}
wildCardChar = "%";
prefix1999 = "'";
suffix1999 = "'";
prefix2000 = "'";
suffix2000 = "'";
break;
}
break;
case "Microsoft OLE DB Provider for SQL Server":
case "Microsoft OLE DB Provider for Jet":
wildCardChar = "%";
prefix1999 = "'";
suffix1999 = "'";
prefix2000 = "'";
suffix2000 = "'";
break;
case "Microsoft OLE DB Provider for Oracle":
wildCardChar = "%";
prefix1999 = "to_date('";
suffix1999 = "','yyyy/mm/dd hh24:mi:ss')";
prefix2000 = "to_date('";
suffix2000 = "','yyyy/mm/dd hh24:mi:ss')";
break;
default:
if( RuntimeDebug ){
RuntimeDebugMessage( "Unsupported ODBC driver: \"Provider Friendly Name\" = [" + connection.Properties.Item("Provider Friendly Name") + "]<br>\n" );
}
wildCardChar = "%";
prefix1999 = "'";
suffix1999 = "'";
prefix2000 = "'";
suffix2000 = "'";
break;
}
switch( Number( datatype )){
case adEmpty:
if( mode == 0 ) retString = "''";
else retString = " IS NULL";
break;
case adBigInt:
case adDecimal:
case adDouble:
case adInterger:
case adNumeric:
case adSingle:
case adSmallInt:
case adTinyInt:
case adUnsignedBigInt:
case adUnsignedInt:
case adUnsignedSmallInt:
case adUnsignedTinyInt:
case adVarNumeric:
if( mode == 0 ) retString = tempValue;
else retString = "=" + tempValue;
break;
case adDate:
case adDBDate:
case adDBTime:
case adDBTimeStamp:
var checkYear = new Date( value );
if( mode == 0 ){
if( checkYear.getYear() <= 99 ) retString = prefix1999 + tempValue + suffix1999;
else retString = prefix2000 + tempValue + suffix2000;
}else{
if(( tempValue == "" )||( tempValue.toUpperCase() == "NULL" )) retString = " IS NULL";
else{
if( checkYear.getYear() <= 99 ) retString = "=" + prefix1999 + tempValue + suffix1999;
else retString = "=" + prefix2000 + tempValue + suffix2000;
}
}
break;
case adBSTR:
case adChar:
case adWChar:
case adVarChar:
case adVarWChar:
if( mode == 0 ) retString = "'" + tempValue + "'";
else{
if( tempValue.toUpperCase() == "NULL" ) retString = " IS NULL";
else if( tempValue == "" ) retString = "=''";
else retString = " like '" + tempValue + wildCardChar + "'";
}
break;
case adLongVarChar: // Access Memo type / Oracle long text
case adLongVarWChar:// MS-SQL long Unicode string
if( mode == 0 ) retString = "'" + tempValue + "'";
else retString = ""; // cannot use at Where clause
break;
default:
if( RuntimeDebug ){
RuntimeDebugMessage( "<hr><h1>Unsupported datatype</h1><br>\n" );
RuntimeDebugMessage( "datatype = [" + datatype + "]<br>value=[" + value + "]<hr>\n" );
}
break;
}
return retString;
}
function makeFieldWhere( connection, key, datatypeSet, datatypeCount )
{
var rawKeys = String( key ).split( "&" );
var whereString = "";
var counter = 0;
for( var i = 0; i < rawKeys.length; i++ ){
var keyDelim = rawKeys[i].indexOf( "=" );
var keyName = unescape( rawKeys[i].substring( 0, keyDelim ));
var keyValue = unescape( rawKeys[i].substring( keyDelim + 1 ));
var datatype = 0;
if( keyName == "" ) continue;
for( var j = 0; j < datatypeCount; j++ ){
if( keyName == datatypeSet[j].name ){
datatype = datatypeSet[j].type;
break;
}
}
var tempValue = keyValue;
var tempString = wrapSQLFieldValue( connection, tempValue, datatype, 1 );
if( tempString == "" ) continue;
if( counter == 0 ) whereString = keyName + tempString;
else whereString += " AND " + keyName + tempString;
counter++;
}
return whereString;
}
function getDatatypeSet( datatypeSet )
{
var datatypeCount = 0;
var rawDatatypes = unobscure( Request.Form( "$datatypes" ));
var datatypes = rawDatatypes.split( '&' );
for( var i = 0; i < datatypes.length; i++ ){
var indexOf = datatypes[i].indexOf( "=" );
if( indexOf <= 0 ) continue;
datatypeSet[datatypeCount] = new Object;
datatypeSet[datatypeCount].name = datatypes[i].substring( 0, indexOf );
datatypeSet[datatypeCount].type = datatypes[i].substring( indexOf+1, datatypes[i].length );
datatypeCount++;
}
return datatypeCount;
}
function getDataFromFormData( data, datatypeSet, datatypeCount )
{
var dataCount = 1;
data[0] = new Object;
data[0].fieldCount = 0;
data[0].paramArray = 0;
data[0].field = new Array;
var args = String( Request.Form ).split( "&" );
for( var counter = 0; counter < args.length; counter++ ){
var regExp = /\+/i; // "+"
while( args[counter].match( regExp ) != null ){
args[counter] = args[counter].replace( regExp, "%20" );
}
args[counter] = unescape( args[counter] );
if( args[counter].charAt( 0 ) == '$' ){
continue;
}
var pointEqual = args[counter].indexOf( "=", 0 );
if( pointEqual == -1 ){
continue;
}
var pointBlaketStart = args[counter].lastIndexOf( "[", pointEqual );
var pointBlaketEnd = args[counter].lastIndexOf( "]", pointEqual );
var arrayNumber = 0;
var paramName = "";
var paramValue = "";
if(( pointBlaketStart == -1 )&&( pointBlaketEnd == -1 )){ // no []
paramName = args[counter].substring( 0, pointEqual );
paramValue = args[counter].substring( pointEqual+1, args[counter].length );
}else if( pointBlaketEnd <= pointBlaketStart ){ // error
paramName = args[counter].substring( 0, pointBlaketStart-1 );
continue;
}else{ // [] case
arrayNumber = Number( args[counter].substring( pointBlaketStart+1, pointBlaketEnd ));
paramName = args[counter].substring( 0, pointBlaketStart );
paramValue = args[counter].substring( pointEqual+1, args[counter].length );
}
var checkArray = -1;
for( var i = 0; i < dataCount; i++ ){
if( data[i].paramArray == 0 ){
data[i].paramArray = arrayNumber;
checkArray = i;
break;
}
if( data[i].paramArray == arrayNumber ){
checkArray = i;
break;
}
}
if( checkArray == -1 ){
data[dataCount] = new Object;
data[dataCount].fieldCount = 0;
data[dataCount].paramArray = arrayNumber;
data[dataCount].field = new Array;
checkArray = dataCount;
dataCount++;
}
data[checkArray].field[data[checkArray].fieldCount] = new Object;
data[checkArray].field[data[checkArray].fieldCount].name = paramName;
data[checkArray].field[data[checkArray].fieldCount].value = paramValue;
var comp01 = paramName.toUpperCase();
for( var j = 0; j < datatypeCount; j++ ){
var comp02 = datatypeSet[j].name.toUpperCase();
if( comp01 == comp02 ){
data[checkArray].field[data[checkArray].fieldCount].datatype = datatypeSet[j].type;
break;
}
}
data[checkArray].fieldCount++;
}
return dataCount;
}
function getKeyFromFormData( key, data, dataCount )
{
var keyCount = 0;
if( dataCount == 1 ){
key[0] = new Object;
key[0].key = unescape( Request.Form( "$key" ));
key[0].paramArray = 1;
data[0].paramArray = 1;
keyCount = 1;
}else{
for( var i = 0; i < dataCount; i++ ){
var keyName = "$key[" + data[i].paramArray + "]";
if( Request.QueryString( keyName ).Count >= 1) {
keyCount = i;
break;
}
key[i] = new Object;
key[i].key = unescape( Request.Form( keyName ));
key[i].paramArray = data[i].paramArray;
}
if( keyCount == 0 ) keyCount = dataCount;
}
return keyCount;
}
function updateUsingPostDataUpdateQuery( connection, tableName )
{
var datatypeSet = new Array;
var datatypeCount = getDatatypeSet( datatypeSet );
var data = new Array;
var dataCount = getDataFromFormData( data, datatypeSet, datatypeCount );
var key = new Array;
var keyCount = getKeyFromFormData( key, data, dataCount );
for( var i = 0; i < dataCount; i++ ){
var matchParamArray;
for( matchParamArray = 0; matchParamArray < keyCount; matchParamArray++ ){
if( key[matchParamArray].paramArray == data[i].paramArray ){
break;
}
}
var whereString = makeFieldWhere( connection, key[matchParamArray].key, datatypeSet, datatypeCount );
var setString = "";
for( var j = 0; j < data[i].fieldCount; j++ ){
var tempString = wrapSQLFieldValue( connection, data[i].field[j].value, data[i].field[j].datatype, 0 );
if( tempString == "" ) continue;
if( j == 0 ) setString = data[i].field[j].name + "=" + tempString;
else setString += ", " + data[i].field[j].name + "=" + tempString;
}
var sqlCommand = "UPDATE " + tableName + " SET " + setString + " WHERE " + whereString;
if( RuntimeDebug ){
RuntimeDebugMessage( "SQL Command = [" + sqlCommand + "]" );
}
var result = connection.Execute( sqlCommand );
}
return;
}
function deleteUsingPostDataDeleteQuery( connection, tableName )
{
var datatypeSet = new Array;
var datatypeCount = getDatatypeSet( datatypeSet );
var key = unescape( Request.Form( "$key" ));
var whereString = makeFieldWhere( connection, key, datatypeSet, datatypeCount );
if( whereString.length != 0 ){
var sqlCommand = "DELETE FROM " + tableName + " WHERE " + whereString;
if( RuntimeDebug ){
RuntimeDebugMessage( "SQL Command = [" + sqlCommand + "]" );
}
var result=connection.Execute( sqlCommand );
}
return;
}
function addRecordUsingPostDataInsertQuery( connection, tableName )
{
var datatypeSet = new Array;
var datatypeCount = getDatatypeSet( datatypeSet );
var data = new Array;
var dataCount = getDataFromFormData( data, datatypeSet, datatypeCount );
var key = new Array;
var keyCount = getKeyFromFormData( key, data, dataCount );
for( var i = 0; i < dataCount; i++ ){
var fieldsString = "";
var valuesString = "";
for( var j = 0; j < data[i].fieldCount; j++ ){
var tempString = wrapSQLFieldValue( connection, data[i].field[j].value, data[i].field[j].datatype, 0 );
if( tempString == "" ) continue;
if((( data[i].field[j].datatype == adDate )||( data[i].field[j].datatype == adDBDate )||( data[i].field[j].datatype == adDBTime )||( data[i].field[j].datatype == adDBTimeStamp ))&&( tempString == "''" )) continue; // datetime datatype
if( j == 0 ){
valuesString = tempString;
fieldsString = data[i].field[j].name;
}else{
valuesString += ", " + tempString;
fieldsString += ", " + data[i].field[j].name;
}
}
var sqlCommand = "INSERT INTO " + tableName + " ( " + fieldsString + " ) VALUES ( " + valuesString + " )";
if( RuntimeDebug ){
RuntimeDebugMessage( "SQL Command = [" + sqlCommand + "]" );
}
var result=connection.Execute( sqlCommand );
}
return;
}
function CSWRecordSet_SetupForm( contentSource )
{
Response.Write( "<input type=\"hidden\" name=\"$connect\" value=\"" + obscure( contentSource.Data.ActiveConnection.ConnectionString ) + "\">" );
Response.Write( "<input type=\"hidden\" name=\"$source\" value=\"" + obscure( contentSource.Data.Source ) + "\">" );
var queryString = String( Request.QueryString );
if( queryString.indexOf( "RECORD_INDEX" ) < 0 ){
queryString = removeUrlParameter( queryString, "$key" );
queryString = removeUrlParameter( queryString, "RECORD_KEY" );
for( var i = 0; i < contentSource.PrimaryKey.length; i++ ) queryString = removeUrlParameter( queryString, contentSource.PrimaryKey[i] );
if( queryString.length == 0 ) queryString += "RECORD_INDEX=" + contentSource.AbsolutePosition;
else queryString += "&RECORD_INDEX=" + contentSource.AbsolutePosition;
}
Response.Write( "<input type=\"hidden\" name=\"$return\" value=\"" + Request( "URL" ) + "?" + queryString + "\">" );
var datatypes = "";
for( var i = 0; i < contentSource.Data.Fields.Count; i++ ){
if( i != 0 ) datatypes += "&";
datatypes += contentSource.Data.Fields.Item( i ).Name + "=" + contentSource.Data.Fields.Item( i ).Type;
}
Response.Write("<input type=\"hidden\" name=\"$datatypes\" value=\"" + obscure( datatypes ) + "\">");
}
</SCRIPT>